home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / StatusScroller.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  10.9 KB  |  433 lines

  1. package symantec.itools.awt.util;
  2.  
  3. import java.applet.*;
  4.  
  5. /**
  6.  * @version 1.0, April 11, 1997
  7.  * @author Symantec
  8.  */
  9.  
  10. //  Created and implemented by Levi Brown, Symantec Macintosh Internet Tools.
  11. //     04/11/97    LAB    Checked it in
  12. //     04/17/97    LAB    Changed a few names to comply with Java standards.
  13. //                Added automatic initialization of the applet context in the constructor.
  14. //                Changed the function comment style to comply with JavaDoc standards.
  15. //                Took out some unnecessary code in setAppletContext(Applet a).
  16. //                Reorganized the code, to group public and private methods separately.
  17. //                Removed some unnecessary import statements.
  18. //                Fixed a bug in setAppletContext(Applet a) that could cause a NullPointerException.
  19. //                Added set/getAutoStart(boolean f) to allow the scrolling to occur without a direct call to start().
  20. //                Changed the default state of execute to true, so scrolling would automatically occur.
  21.  
  22. public class StatusScroller implements Runnable
  23. {
  24.     protected boolean isRightToLeft, isRepeat, isScrollClean;
  25.     protected AppletContext context;
  26.     protected Thread thread;
  27.     protected int delay, wblength, sslength, index;
  28.     protected    String    statusString;
  29.     protected StringBuffer    workingBuffer;
  30.     
  31.     private boolean execute, loopFlag;
  32.     
  33.     /**
  34.      * Default Constructor
  35.      */
  36.     public StatusScroller()
  37.     {
  38.         isRightToLeft    = true;
  39.         isRepeat        = true;
  40.         isScrollClean    = true;
  41.         execute            = true;
  42.         loopFlag        = false;
  43.         statusString    = null;
  44.         delay            = 100;
  45.         thread            = new Thread(this);
  46.         
  47.         //Initalize the context variable
  48.         setAppletContext(symantec.itools.lang.Context.getApplet());
  49.  
  50.         thread.start();
  51.     }
  52.  
  53.  
  54.     //***** Public Methods *****//
  55.  
  56.     //start
  57.     /**
  58.      * Start the status text scrolling
  59.      * @see #run
  60.      */
  61.     public void start()
  62.     {
  63.         execute = true;
  64.         thread.resume();
  65.     }
  66.  
  67.     //stop
  68.     /**
  69.      * Stops the status text from scrolling
  70.      * @see #start
  71.      */
  72.     public void stop()
  73.     {
  74.         execute   = false;
  75.     }
  76.  
  77.     //clear
  78.     /**
  79.      * Clears the status area
  80.      */
  81.     public void clear()
  82.     {
  83.         if(context != null)
  84.         {
  85.             //Display an empty string
  86.             context.showStatus("");
  87.         }
  88.     }
  89.     
  90.     //setString
  91.     /**
  92.      * Sets the string to use for scrolling
  93.      * @see #getString
  94.      * @param String s
  95.      */
  96.     public void setString(String s)
  97.     {
  98.         statusString = s;
  99.         index = 0;
  100.  
  101.         if(statusString == null || statusString.equals(""))
  102.         {
  103.             statusString =    null;
  104.             workingBuffer = null;
  105.             wblength = 0;
  106.             sslength = 0;
  107.             if(execute)
  108.                 clear();
  109.         }
  110.         else
  111.         {
  112.             sslength = statusString.length();
  113.             updateWorkingBuffer();
  114.         }
  115.     }
  116.  
  117.     //getString
  118.     /**
  119.      * The string currently being used for scrolling
  120.      * @see #setString
  121.      * @return String
  122.      */
  123.     public String getString()
  124.     {
  125.         return statusString;
  126.     }
  127.  
  128.     //setRightToLeft
  129.     /**
  130.      * Controlls the direction the text will scroll.
  131.      * If true, the text will scroll from the Right to the Left;
  132.      * if false, the text will scroll from the Left to the Right.
  133.      * @see #getRightToLeft
  134.      * @param boolean b
  135.      */
  136.     public void setRightToLeft(boolean b)
  137.     {
  138.         isRightToLeft = b;
  139.     }
  140.  
  141.     //getRightToLeft
  142.     /**
  143.      * The direction the text will scroll.
  144.      * If true, the text will scroll from the Right to the Left;
  145.      * if false, the text will scroll from the Left to the Right.
  146.      * @see #setRightToLeft
  147.      * @return boolean
  148.      */
  149.     public boolean getRightToLeft()
  150.     {
  151.         return isRightToLeft;
  152.     }
  153.  
  154.     //setScrollClear
  155.     /**
  156.      * Controlls one behavior of the scrolling.
  157.      * If true, the text will scroll completely off before scrolling on again;
  158.      * if false, the text will scroll from head to tail or tail to head
  159.      * (depending on the direction) directly.
  160.      * @see #getScrollClean
  161.      * @see #getRightToLeft
  162.      * @param boolean b
  163.      */
  164.     public void setScrollClean(boolean b)
  165.     {
  166.         isScrollClean = b;
  167.         
  168.         updateWorkingBuffer();
  169.     }
  170.  
  171.     //getScrollClear
  172.     /**
  173.      * One behavior of the scrolling.
  174.      * If true, the text will scroll completely off before scrolling on again;
  175.      * if false, the text will scroll from head to tail or tail to head
  176.      * (depending on the direction) directly.
  177.      * @see #setScrollClean
  178.      * @see #getRightToLeft
  179.      * @return boolean
  180.      */
  181.     public boolean getScrollClean()
  182.     {
  183.         return isScrollClean;
  184.     }
  185.  
  186.     //setAutoStart
  187.     /**
  188.      * Determines if the scrolling will automatically start when the applet is loaded.
  189.      * If true the text will start scrolling as soon as the applet is loaded;
  190.      * if false, the text will not scroll until start() is called.
  191.      * @see #getAutoStart
  192.      * @see #start
  193.      * @see #stop
  194.      * @param boolean f
  195.      */
  196.     public void setAutoStart(boolean f)
  197.     {
  198.         if(f)
  199.             start();
  200.         else
  201.             stop();
  202.     }
  203.  
  204.     //getAutoStart
  205.     /**
  206.      * Determines if the scrolling will automatically start when the applet is loaded.
  207.      * If true the text will start scrolling as soon as the applet is loaded;
  208.      * if false, the text will not scroll until start() is called.
  209.      * @see #setAutoStart
  210.      * @see #start
  211.      * @see #stop
  212.      * @return boolean Wether or not the text will scroll as soon as the applet is loaded.
  213.      */
  214.     public boolean getAutoStart()
  215.     {
  216.         return execute;
  217.     }
  218.     
  219.     //setRepeat
  220.     /**
  221.      * Controlls one behavior of the scrolling
  222.      * If true the text will continue scrolling over and over;
  223.      * if false, the text will scroll off and back on, then stop.
  224.      * @see #getRepeat
  225.      * @param boolean f
  226.      */
  227.     public void setRepeat(boolean f)
  228.     {
  229.         isRepeat = f;
  230.     }
  231.  
  232.     //getRepeat
  233.     /**
  234.      * One behavior of the scrolling
  235.      * If true the text will continue scrolling over and over;
  236.      * if false, the text will scroll off and back on, then stop.
  237.      * @see #setRepeat
  238.      * @return boolean
  239.      */
  240.     public boolean getRepeat()
  241.     {
  242.         return isRepeat;
  243.     }
  244.  
  245.     //setDelay
  246.     /**
  247.      * Set the time between the display of each character in milliseconds.
  248.      * Minimum delay of 30 miliseconds.
  249.      * @see #getDelay
  250.      * @param int d
  251.      */
  252.     public void setDelay(int d)
  253.     {
  254.         if(d < 30)
  255.             delay = 30;
  256.         else
  257.             delay = d;
  258.     }
  259.  
  260.     //getDelay
  261.     /**
  262.      * The time between the display of each character in milliseconds
  263.      * @see #setDelay
  264.      * @return int
  265.      */
  266.     public int getDelay()
  267.     {
  268.         return delay;
  269.     }
  270.  
  271.     //setAppletContext
  272.     /**
  273.      * StatusScroller needs an AppletContext in order to set the status area
  274.      * This version takes an AppletContext object directly.
  275.      * @see #setAppletContext(Applet)
  276.      * @param AppletContext c
  277.      */
  278.     public void setAppletContext(AppletContext c)
  279.     {
  280.         context = c;
  281.     }
  282.     
  283.     //setAppletContext
  284.     /**
  285.      * StatusScroller needs an AppletContext in order to set the status area
  286.      * This version takes an Applet object and gets the needed AppletContext from it.
  287.      * @see #setAppletContext(AppletContext)
  288.      * @param Applet a
  289.      */
  290.     public void setAppletContext(Applet a)
  291.     {
  292.         if(a == null)
  293.             context = null;
  294.         else
  295.             context = a.getAppletContext();
  296.     }
  297.  
  298.     //run
  299.     /**
  300.      * The main logic loop
  301.      */
  302.     public void run()
  303.     {
  304.         String workingString = null;
  305.         int loopIndicator;
  306.         
  307.         if(!execute) thread.suspend();
  308.         try
  309.         {
  310.             while(true)
  311.             {
  312.                 do
  313.                 {
  314.                        //loopIndicator = index;
  315.                         
  316.                     thread.sleep(delay);
  317.  
  318.                     if (execute)
  319.                     {
  320.                         //Get the string to display
  321.                         workingString = scrollString();
  322.                         if (context != null && workingString != null)
  323.                         {
  324.                             //Display the string
  325.                             context.showStatus(workingString);
  326.                         }
  327.                     }
  328.                 }
  329.                 //Keep scrolling until an exit condition is met
  330.                 while (index != 0 || isRepeat);
  331.                 
  332.                 thread.suspend();
  333.             }
  334.         }
  335.         catch (InterruptedException e)
  336.         {
  337.         }
  338.     }
  339.     
  340.        //***** Private Methods *****//
  341.     
  342.     //scrollString
  343.     /**
  344.      * Handles the manipulation of the string buffer to give the
  345.      * desired scrolling effect.
  346.      * @see #run
  347.      * @see #setRightToLeft
  348.      * @see #getRightToLeft
  349.      * @return String
  350.      */
  351.     protected String scrollString()
  352.     {
  353.         //If the string is null
  354.         if(workingBuffer == null)
  355.             return null;
  356.         //If there is only one character
  357.         if(wblength < 2)
  358.             return(workingBuffer.toString());
  359.         
  360.         char ch, temp[];
  361.         //Allocate space for the rest of the characters
  362.         temp = new char[wblength - 1];
  363.         
  364.         if(isRightToLeft)
  365.         {                
  366.             //Store the first character
  367.             ch = workingBuffer.charAt(0);
  368.             //Store the rest of the characters
  369.             workingBuffer.getChars(1, wblength, temp, 0);
  370.             //Shift the rest of the characters to the left one index
  371.             workingBuffer = new StringBuffer(new String(temp));
  372.             //Append the first character
  373.             workingBuffer = workingBuffer.append(ch);
  374.             //Keep track of where the original first character is
  375.             index++;
  376.             if(index > wblength -1)
  377.                 index = 0;
  378.         }
  379.         else
  380.         {
  381.             //Store the last character
  382.             ch = workingBuffer.charAt(wblength - 1);
  383.             //Store the rest of the characters
  384.             workingBuffer.getChars(0, wblength - 1, temp, 0);
  385.             //Place the last character at the beginning
  386.             workingBuffer = new StringBuffer("" + ch);
  387.             //Append the rest of the characters
  388.             workingBuffer = workingBuffer.append(temp);
  389.             //Keep track of where the original first character is
  390.             index--;
  391.             if(index < 0)
  392.                 index = wblength -1;
  393.         }
  394.         
  395.         //Return a window onto the modified string buffer to be displayed
  396.         return((workingBuffer.toString()).substring(0, sslength));
  397.     }
  398.  
  399.     //makePadding
  400.     /**
  401.      * Returns a String with howBig number of spaces as the content
  402.      * @see #updateWorkingBuffer
  403.      * @param int howBig
  404.      * @returns String
  405.      */
  406.     protected String makePadding(int howBig)
  407.     {
  408.         StringBuffer str = new StringBuffer(0);
  409.          
  410.         for(int i = 0; i < howBig; ++i)
  411.             str.append(" ");
  412.         return(str.toString());
  413.     }
  414.  
  415.     //updateWorkingBuffer
  416.     /**
  417.      * Updates the string buffer depending on the settings
  418.      * @see #setScrollClean
  419.      */
  420.     protected void updateWorkingBuffer()
  421.     {        
  422.         if(isScrollClean)
  423.         {
  424.             String temp = makePadding(sslength);
  425.             workingBuffer = new StringBuffer(statusString + temp);
  426.         }
  427.         else
  428.             workingBuffer = new StringBuffer(statusString);
  429.             
  430.         wblength = workingBuffer.length();
  431.     }
  432.         
  433. }